//
//  MyBalanceViewController.m
//  BaseProject
//
//  Created by 王洋 on 2020/8/8.
//  Copyright © 2020 王洋. All rights reserved.
//
#import "TT_BaseNaviView.h"

#import "MyBalanceHeaderView.h"

#import "MyBalanceListCell.h"

#import "CommonBalanceModel.h"
#import "UserBalanceInfoModel.h"


#import "MyBalanceSectionHeader.h"
#import "WithdrawListViewController.h"

#import "WithdrawViewController.h"
#import "MyBalanceViewController.h"

@interface MyBalanceViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableview;

@property (nonatomic, strong) TT_BaseNaviView *naviView;

@property (nonatomic, strong) MyBalanceHeaderView *headView;


@property (nonatomic, strong) UserBalanceInfoModel *headModel;

@property (nonatomic, strong) NSMutableArray <CommonBalanceModel *>*dataArray;

@end

@implementation MyBalanceViewController

- (void)viewDidLoad {
    [super viewDidLoad];


    [self initView];
    [self request_balance];
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
}

- (void)initView {
    
    self.navigationController.navigationBarHidden = YES;
    #if RT_INTERACTIVE_PUSH
        self.rt_navigationController.rt_enableInteractivePush = YES;
    #endif
    self.rt_disableInteractivePop = NO;

    [self.view addSubview:self.headView];
    [self.headView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo(0);
        make.height.mas_equalTo(160 + NavBar_Height + kStatusBarHeight);
    }];
    
    [self.view addSubview:self.naviView];
    [self.naviView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo(0);
        make.height.mas_equalTo(NavBar_Height + kStatusBarHeight);

    }];
    
    UIButton *detailBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [detailBtn setTitle:@"提现明细" forState:UIControlStateNormal];
    detailBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    [detailBtn setTitleColor:UIColorHex(0xffffff) forState:UIControlStateNormal];
    [detailBtn addTarget:self action:@selector(goDetail) forControlEvents:UIControlEventTouchUpInside];
    [self.naviView addSubview:detailBtn];
    [self.naviView.backBtn setImage:[UIImage imageNamed:@"public_back_white"] forState:UIControlStateNormal];
    
    [detailBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.naviView.titleLab);
        make.right.mas_equalTo(-15);
        make.height.mas_equalTo(44);
//        make.width.mas_equalTo(50);
    }];
    
    
    [self.view addSubview:self.tableview];
    [self.tableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.headView.mas_bottom);
        make.left.right.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
    }];
    
    [self.tableview reloadData];
    
    WEAKSELF
    self.headView.MyBalanceHeaderViewBlock = ^{
        
        WithdrawViewController *vc = [[WithdrawViewController alloc] init];
        vc.money = weakSelf.headModel.availableAmount;
        [weakSelf.navigationController pushViewController:vc animated:YES];
    };
}

- (void)goDetail {
    WithdrawListViewController *vc = [[WithdrawListViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)goBack {
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark UITableviewDelegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 60;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 40;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

#pragma mark sectionheader
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    MyBalanceSectionHeader *headView = [[NSBundle mainBundle] loadNibNamed:@"MyBalanceSectionHeader"owner:nil options:nil].firstObject;
    
    return  headView;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    MyBalanceListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyBalanceListCellID"];
    cell.selectionStyle =UITableViewCellSelectionStyleNone;
    [cell fillCellWithModel:self.dataArray[indexPath.row]];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    

}

- (UITableView *)tableview {
    if (!_tableview) {
        _tableview = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableview.delegate = self;
        _tableview.dataSource = self;
        _tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableview.backgroundColor =  UIColorHex(0xf1f1f1);
        
        _tableview.showsHorizontalScrollIndicator = NO;
        _tableview.showsVerticalScrollIndicator = NO;
        
        _tableview.estimatedSectionHeaderHeight = 0;
        _tableview.estimatedSectionFooterHeight = 0;
        _tableview.estimatedRowHeight = 0;
        
        [_tableview registerNib:[UINib nibWithNibName:@"MyBalanceListCell" bundle:nil] forCellReuseIdentifier:@"MyBalanceListCellID"];
    }
    return _tableview;
}

- (NSMutableArray<CommonBalanceModel *> *)dataArray {
    if (!_dataArray) {
        _dataArray = @[].mutableCopy;
    }
    return _dataArray;
}

- (MyBalanceHeaderView *)headView {
    if (!_headView) {
        _headView = [[NSBundle mainBundle] loadNibNamed:@"MyBalanceHeaderView"owner:nil options:nil].firstObject;
    }
    return _headView;
}

- (TT_BaseNaviView *)naviView {
    if (!_naviView) {
        _naviView = [[NSBundle mainBundle] loadNibNamed:@"TT_BaseNaviView"owner:nil options:nil].firstObject;
        _naviView.titleLab.text = @"我的余额";
        _naviView.titleLab.textColor = UIColorHex(0xffffff);
        [_naviView.backBtn addTarget:self action:@selector(goBack) forControlEvents:UIControlEventTouchUpInside];
    }
    return _naviView;
}

///v1/userpc
- (void)request_balance {
    
    NSMutableDictionary *dic = @{}.mutableCopy;
    [dic setValue:[TTUserModel sharedObject].userId forKey:@"userId"];

    NSString *url = [NSString stringWithFormat:@"%@%@",TT_Global_Api_Domain,API_MyBalance];
    
    WEAKSELF
    [TTHttpManager postRequestWithUrl:url TimeOut:10 PostData:dic FinishBlock:^(id  _Nonnull data) {
        
        if ([data isKindOfClass:[NSDictionary class]]) {

            //请求成功
            NSInteger code = [[data valueForKey:@"code"] integerValue];
            if (code == 10000) {
                
                NSDictionary *resultDic = [data valueForKey:@"data"];
                if (![resultDic isKindOfClass:[NSDictionary class]]) {
                    return;
                }
                NSArray *balanceSubsidiary = [resultDic valueForKey:@"balanceSubsidiary"];
                NSDictionary *user = [resultDic valueForKey:@"user"];
                
                weakSelf.headModel = [UserBalanceInfoModel modelWithDictionary:user];
                
                if ([balanceSubsidiary isKindOfClass:[NSArray class]]) {
                    weakSelf.dataArray = @[].mutableCopy;
                    for (int i = 0; i < balanceSubsidiary.count; i++) {
                        NSDictionary *balanceSubsidiaryDic = balanceSubsidiary[i];
                        CommonBalanceModel *model = [CommonBalanceModel modelWithDictionary:balanceSubsidiaryDic];
                        [weakSelf.dataArray addObject:model];
                    }
                }
                
                weakSelf.headView.tixianLab.text =[Utilities changeDoubleToString:weakSelf.headModel.withdrawalAmount];
                weakSelf.headView.dongjieLab.text =[Utilities changeDoubleToString:weakSelf.headModel.iceAmount];
                weakSelf.headView.keyongLab.text =[Utilities changeDoubleToString:weakSelf.headModel.availableAmount];
                weakSelf.headView.zongjineLab.text =[Utilities changeDoubleToString:weakSelf.headModel.totalAmount];

                [weakSelf.tableview reloadData];
                
            } else {
               [SVProgressHUD showErrorWithStatus:[data valueForKey:@"message"]];
               [SVProgressHUD dismissWithDelay:1];
           }
        }
        
    } FailedBlock:^(NSError * _Nonnull error) {
        
    }];
}

@end
